package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.saveOrUpdateApartmentInfo(apartmentSubmitVo);

//        apartmentInfoService.saveOrUpdateApartmentInfo(apartmentSubmitVo);

        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        /*//先根据条件查询
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装查询条件
        //省
        apartmentInfoLambdaQueryWrapper.eq(queryVo.getDistrictId() != null, ApartmentInfo::getDistrictId, queryVo.getDistrictId());
        //市
        apartmentInfoLambdaQueryWrapper.eq(queryVo.getCityId()!=null,ApartmentInfo::getCityId,queryVo.getCityId());
        //县
        apartmentInfoLambdaQueryWrapper.eq(queryVo.getProvinceId()!=null,ApartmentInfo::getProvinceId,queryVo.getProvinceId());
        //查询所有
       // List<ApartmentInfo> list = apartmentInfoService.list(apartmentInfoLambdaQueryWrapper);
        //调用自带的分页方法
        Page<ApartmentInfo> apartmentInfoPage = new Page<>(current,size);
        apartmentInfoService.page(apartmentInfoPage,apartmentInfoLambdaQueryWrapper);

        return Result.ok();*/
/**
 * 上面只是将所有数据查了出来,摒弃不用
 * 下面才是进行分页查询
 *
 * 想要分页查询就得写分页的配置
 * 首先创建一个page对象对分页的数据进行封装
 * 下面自定义sql写查询方法
 * 依旧会用到分页插件
 */
//创建分页插件
        Page<ApartmentItemVo> apartmentItemVoPage = new Page<>(current, size);
        //自定义查询分页方法
        IPage<ApartmentItemVo> iPage = apartmentInfoService.getPageList(apartmentItemVoPage,queryVo);

        return Result.ok(iPage);
    }


    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        ApartmentDetailVo getApartmentInfoById = apartmentInfoService.getApartmentInfoById(id);

        return Result.ok();
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {

        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();

        apartmentInfoLambdaQueryWrapper.eq(ApartmentInfo::getId, id);

        LambdaUpdateWrapper<ApartmentInfo> apartmentInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        apartmentInfoLambdaUpdateWrapper.set(status != null, ApartmentInfo::getIsRelease, status);

        apartmentInfoService.update(apartmentInfoLambdaUpdateWrapper);

        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {

        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper<ApartmentInfo> eq = apartmentInfoLambdaQueryWrapper.eq(id != null, ApartmentInfo::getDistrictId, id);

        List<ApartmentInfo> list = apartmentInfoService.list(eq);
        return Result.ok(list);
    }
}














